﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using System;
using System.IO;

namespace WebApiExtend.Middleware
{
    /// <summary>
    /// 中间件
    /// </summary>
    public static class MiddlewareExtension
    {
        /// <summary>
        /// 日志映射,兼容所有平台
        /// </summary>
        /// <param name="app">要进行扩展的类型</param>
        public static void ExUseMappingFolder(this IApplicationBuilder app)
        {
            //Set up custom content types - associating file extension to MIME type
            var provider = new FileExtensionContentTypeProvider();

            var logPath = $"{AppContext.BaseDirectory}logs";
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            //添加映射
            provider.Mappings[".log"] = "text/plain;charset=utf-8";
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(logPath),
                RequestPath = "/logs",
                ContentTypeProvider = provider
            });

            app.UseDirectoryBrowser(new DirectoryBrowserOptions
            {
                FileProvider = new PhysicalFileProvider(logPath),
                RequestPath = "/logs"
            });
        }


    }
}
